package be.novelfaces.showcase;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import com.google.common.collect.Lists;

@ManagedBean
@ViewScoped
public class DragAndDropManagedBean implements Serializable {
	
	private List<String> elements = Lists.newArrayList("A", "B", "C");
	
	private String src;
	private String dest;
	
	public void dragend(AjaxBehaviorEvent event) {
		if (src != null & dest != null & !src.equals(dest)) {
			String message = "You dragged "+ src + " to " + dest + " (swapping the two).";
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,message , message));
			Collections.swap(elements, elements.indexOf(src),
					elements.indexOf(dest));
		}
	}

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	public String getDest() {
		return dest;
	}

	public void setDest(String dest) {
		this.dest = dest;
	}
	
	public List<String> getElements() {
		return elements;
	}
	
}
